🏪

لوحة المطعم

ادخل برابط مطعمك وكلمة المرور

`); w.document.close(); $('qr-note').textContent=`جُهّز ${n} باركود — نافذة الطباعة مفتوحة.`; } function pickLogo(){pickImage(d=>{logoImg=d;$('set-logo').innerHTML=``;});} function pickCover(){pickImage(d=>{coverImg=d;$('set-cover').innerHTML=``;});} async function saveSettings(){ const body={name:$('set-name').value.trim(),tagline:$('set-tag').value.trim(),phone:$('set-phone').value.trim(), themeColor:$('set-colorHex').value.trim(),address:$('set-address').value.trim(),currency:$('set-currency').value.trim(), orderModes:[...document.querySelectorAll('.set-mode:checked')].map(c=>c.value),allowCall:$('set-call').checked,tables:$('set-tables').value}; if(logoImg)body.logo=logoImg;if(coverImg)body.cover=coverImg; const d=await jp('/api/admin/settings','PUT',body);if(!d.ok)return toast('❌ '+(d.err||'فشل')); R=d.restaurant;applyAccent();$('t-name').textContent=R.name;if(R.logo)$('t-logo').innerHTML=``; toast('✅ حُفظت الإعدادات');} async function changePass(){const np=$('set-pass').value.trim();if(np.length<4)return toast('⚠️ 4 أحرف على الأقلّ'); const d=await jp('/api/admin/password','PUT',{newPassword:np});if(!d.ok)return toast('❌ '+(d.err||'فشل'));$('set-pass').value='';toast('✅ حُدّثت كلمة المرور');} /* ═══ الإحصائيّات ═══ */ async function loadStats(){const d=await j('/api/admin/stats');if(!d.ok)return;const s=d.stats;const cur=s.currency; const maxRev=Math.max(1,...s.last7.map(x=>x.rev)); const chLabel={table:'🍽️ طاولة',staff:'🧑‍🍳 موظّف',delivery:'🛵 توصيل',call:'🔔 نداء'}; $('p-stats').innerHTML=`

📊 إحصائيّات المطعم

${kpi('💰',money(s.todayRev)+' '+cur,'مبيعات اليوم')} ${kpi('🧾',s.todayCount,'طلبات اليوم')} ${kpi('📈',money(s.paidRev)+' '+cur,'إجمالي المبيعات')} ${kpi('✅',s.paidCount,'طلبات مكتملة')} ${kpi('⏳',money(s.activeVal)+' '+cur,'قيد التحصيل ('+s.activeCount+')')} ${kpi('🧮',money(s.avg)+' '+cur,'متوسّط الطلب')}

📅 مبيعات آخر ٧ أيّام

${s.last7.map(x=>`
${x.rev?money(x.rev):''}
${x.date.slice(5)}
`).join('')}

🏆 الأصناف الأكثر مبيعاً

${s.topItems.length?s.topItems.map((it,i)=>`
${i+1}${esc(it.name)}${it.qty}×${money(it.rev)}
`).join(''):'
لا مبيعات بعد
'}

🧭 الطلبات حسب القناة

${Object.entries(s.byChannel).map(([k,v])=>`
${v}${chLabel[k]||k}
`).join('')}
`; } function kpi(i,n,l){return `
${i}
${n}
${l}
`;} /* ═══ معاينة الواجهات (مشاهدة فقط) ═══ */ let previewRole='waiter',PORDERS=[]; async function loadPreview(){const d=await j('/api/admin/orders?active=1');if(!d.ok)return;PORDERS=d.orders||[];renderPreview();} function setPreview(r){previewRole=r;renderPreview();} function renderPreview(){ $('p-preview').innerHTML=`

👁️ معاينة الواجهات — مشاهدة فقط

📱 منيو الزبون ↗
`; renderPreviewBody(); } function renderPreviewBody(){const box=$('preview-body'); if(previewRole==='tables'){box.innerHTML=tablesHTML(PORDERS);return;} let list=PORDERS.filter(o=>o.status!=='paid'&&o.status!=='cancelled'); if(previewRole==='kitchen')list=list.filter(o=>o.type!=='call'&&['new','preparing','ready'].includes(o.status)); if(previewRole==='cashier')list=list.filter(o=>o.type!=='call'); box.innerHTML=list.length?'
'+list.map(o=>roCard(o)).join('')+'
':'
لا طلبات نشطة
'; } function roCard(o){const isCall=o.type==='call';const chan=o.channel||'table';const chL={table:'طاولة',staff:'عبر الموظّف',delivery:'توصيل'}[chan]||''; return `
#${o.seq}${isCall?'🔔 نداء':'🍽️ '+(o.table?'طاولة '+esc(o.table):chL)}${ST_LABEL[o.status]||o.status}
${timeAgo(o.createdAt)}${o.byStaff?' · '+esc(o.byStaff):''}
${isCall?'
🔔 نداء نادل
':(o.items||[]).map(it=>`
${it.qty}× ${esc(it.name)}${money(it.price*it.qty)}
`).join('')} ${!isCall?`
الإجمالي${money(o.total)} ${esc(R.currency)}
`:''} ${o.channel==='delivery'?`
🛵 ${esc(o.customerName||'')} · ${esc(o.customerPhone||'')}
`:''}
`; } function tablesHTML(orders){const n=R.tables||0;if(!n)return '
اضبط عدد الطاولات من الإعدادات أولاً
'; const occ={};for(const o of orders){if(o.type==='call'||o.channel==='delivery')continue;if(['paid','cancelled'].includes(o.status))continue;const t=String(o.table||'').trim();if(!t)continue;(occ[t]=occ[t]||[]).push(o);} const busyN=Object.keys(occ).filter(t=>+t>=1&&+t<=n).length; let cells='';for(let i=1;i<=n;i++){const os=occ[i]; if(os&&os.length){const tot=os.reduce((s,o)=>s+o.total,0);cells+=`
طاولة ${i}
🔴 مشغولة
#${os.map(o=>o.seq).join('، #')} · ${money(tot)}
`;} else cells+=`
طاولة ${i}
🟢 متاحة
`;} return `
🟢 متاحة: ${n-busyN} · 🔴 مشغولة: ${busyN}
${cells}
`; } /* ═══ أدوات ═══ */ function pickImage(cb){const i=document.createElement('input');i.type='file';i.accept='image/*';i.onchange=()=>{const f=i.files[0];if(!f)return;const r=new FileReader();r.onload=()=>resizeImg(r.result,cb);r.readAsDataURL(f);};i.click();} function resizeImg(dataUrl,cb){const img=new Image();img.onload=()=>{const max=1000;let w=img.width,h=img.height;if(w>max||h>max){if(w>h){h=Math.round(h*max/w);w=max;}else{w=Math.round(w*max/h);h=max;}}const c=document.createElement('canvas');c.width=w;c.height=h;c.getContext('2d').drawImage(img,0,0,w,h);cb(c.toDataURL('image/jpeg',0.82));};img.src=dataUrl;} function openM(title,body,foot){$('mTitle').textContent=title;$('mBody').innerHTML=body;$('mFoot').innerHTML=foot;$('ov').classList.add('show');} function closeM(){$('ov').classList.remove('show');} let toT;function toast(m){const t=$('toast');t.textContent=m;t.classList.add('show');clearTimeout(toT);toT=setTimeout(()=>t.classList.remove('show'),2400);} boot();